package com.example.lxh.testinfo;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ViewUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

@ContentView(R.layout.activity_volley)
public class VolleyActivity extends AppCompatActivity {

    @ViewInject(R.id.btn)
    private TextView m_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.Ext.init(getApplication());
        x.Ext.setDebug(false);
        //
        RequestQueue queue = Volley.newRequestQueue(VolleyActivity.this);
        StringRequest request = new StringRequest("http://192.168.1.107:8080/Dome/JsonServlet?id=1&type=json", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.w("Lxh", "onResponse 返回： " + response);
                File file = new File("/mnt/sdcard/test.txt");
                if(!file.exists()){
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    FileWriter fileWriter = new FileWriter(file);
                    BufferedWriter bw = new BufferedWriter(fileWriter);
                    bw.write(response);
                    bw.close();
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.w("Lxh", "onErrorResponse: " + error);
            }
        });
        queue.add(request);
    }
}
